package cn.pconline.autoclub.service.bbssync;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;

import cn.pconline.autoclub.entity.Club;
import cn.pconline.autoclub.entity.ClubMember;
import cn.pconline.autoclub.entity.DynaImage;
import cn.pconline.autoclub.entity.User;
import cn.pconline.autoclub.service.DynaService;
import cn.pconline.autoclub.service.PetrolService;
import cn.pconline.autoclub.util.dyna.TopicDyna;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

/**
 * 论坛同步修改帖子到车友会
 * @author zhouruxuan
 * @date 2014年12月4日 上午10:08:18
 */
public class SyncUpdateTopicService extends BbsSyncPetrolService {
	@Autowired
	private DynaService dynaService;
	@Autowired
	private PetrolService petrolService;
	
    @Override
    protected int addPetroltoMember(ClubMember clubMember, JSONObject msg) {
        log.info("update topic not need add petrol to member");
        return 0;
    }

    @Override
    protected int addPetroltoClub(Club club, JSONObject msg) {
        log.info("update topic not need add petrol to club");
        return 0;
    }

    @Override
    protected long getUserId(JSONObject msg) {
        return msg.getLongValue("topicAuthorId");
    }
    
	@Override
	public void process(JSONObject msg, ClubMember clubMember) {
	    
		//验证用户
	    long userId = getUserId(msg);
	    if (userId <= 0L) {
            log.info("not userId-{}", userId);
            return;
        }

        //判断用户是否登录
        User user = userService.authUser(userId);
        if (user == null || user.getUid() == 0L) {
            log.info("not found user-{}", userId);
            return;
        }

        //获取车友会ID
        long clubId = 0;
        if(clubMember != null) {
            clubId = clubMember.getClubId();
        }
		
        //更新帖子
        TopicDyna topicDyna = parseActionLog2DynaInfo(msg, clubId);
        List<DynaImage> dynaImages = parseActionLog2DynaImages(msg, userId);
        dynaService.updateDynaInfo(topicDyna, dynaImages);
	}
	
	/**
	 * @param json转topic
	 * @param clubId
	 * @return
	 */
    private TopicDyna parseActionLog2DynaInfo(JSONObject msg, long clubId) {
        TopicDyna topicDyna = new TopicDyna();
        topicDyna.setTopicId(msg.getLongValue("topicId"));
        topicDyna.setCreateDate(new Date(msg.getLongValue("topicCreateAt")));
        
        topicDyna.setClubId(clubId);
        topicDyna.setForumId(msg.getLongValue("forumId"));
        topicDyna.setUserId(msg.getLongValue("topicAuthorId"));
        topicDyna.setForumName(StringUtils.defaultString(msg.getString("forumName")));
        topicDyna.setLastPostAt(new Date(msg.getLongValue("lastPostAt")));
        //topicDyna.setLastPostBy(msg.getLongValue("topicUserId"));
        //topicDyna.setLastPostName(userService.getNickNameByUserId(msg.getLongValue("topicUserId")));
        topicDyna.setMessage(StringUtils.defaultString(msg.getString("postMessage")));
        topicDyna.setTopicTitle(StringUtils.defaultString(msg.getString("title")));
        topicDyna.setPick(msg.getIntValue("pick"));
        topicDyna.setRecommend(msg.getIntValue("recommend"));
        topicDyna.setPostCount(msg.getIntValue("topicReplyCount"));
        topicDyna.setCreateName(msg.getString("topicAuthorName"));//论坛传的是nickname

        return topicDyna;
    }

    /**
     * 帖子图片列表
     * @param msg
     * @param userId
     * @return
     */
    private List<DynaImage> parseActionLog2DynaImages(JSONObject msg, long userId) {
    	String imageString = msg.getString("images");
        if (imageString == null) {
            return null;
        } else {
            JSONArray jsonArray = JSONArray.parseArray(imageString);
            List<DynaImage> list = new ArrayList<DynaImage>();
            for (int i = 0; i < jsonArray.size(); i++) {
                JSONObject json = JSONObject.parseObject(jsonArray.getString(i));
                DynaImage dynaImage = new DynaImage();
                dynaImage.setSeq(json.getIntValue("seq"));
                dynaImage.setUrl(json.getString("url"));
                dynaImage.setWidth(json.getIntValue("width"));
                dynaImage.setHeight(json.getIntValue("height"));
                if (StringUtils.isBlank(dynaImage.getUrl())) {
                    continue;
                }
                list.add(dynaImage);
            }
            return list;
        }
    }
}
